package com.dupengcheng.subject.domain.hanler.subject;

import com.dupengcheng.subject.common.enums.IsDeleteEnum;
import com.dupengcheng.subject.common.enums.SubjectTypeEnum;
import com.dupengcheng.subject.domain.convert.SubjectAnswerConvert;
import com.dupengcheng.subject.domain.entity.SubjectAnswerBO;
import com.dupengcheng.subject.domain.entity.SubjectInfoBO;
import com.dupengcheng.subject.domain.entity.SubjectOptionBO;
import com.dupengcheng.subject.infra.basic.entity.SubjectJudge;
import com.dupengcheng.subject.infra.basic.service.SubjectJudgeService;
import lombok.Getter;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Component
public class JudgeHandler implements SubjectTypeHandler {

    @Getter
    private SubjectTypeEnum subjectTypeEnum=SubjectTypeEnum.JUDGE;

    @Resource
    private SubjectJudgeService judgeService;

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        SubjectJudge subjectJudge=new SubjectJudge();
        List<SubjectAnswerBO> optionList = subjectInfoBO.getOptionList();
        SubjectAnswerBO subjectAnswerBO = optionList.get(0);
        subjectJudge.setSubjectId(subjectInfoBO.getId());
        subjectJudge.setIsCorrect(subjectAnswerBO.getIsCorrect());
        subjectJudge.setIsDeleted(IsDeleteEnum.UN_DELETE.getCode());
        judgeService.insert(subjectJudge);

    }

    @Override
    public SubjectOptionBO query(Long subjectId) {
        SubjectJudge subjectJudge=new SubjectJudge();
        subjectJudge.setSubjectId(subjectId);
        List<SubjectJudge> subjectJudgeList=judgeService.queryByCondition(subjectJudge);
        List<SubjectAnswerBO> subjectAnswerBOS=SubjectAnswerConvert.INSTANCE.
                convertJudgeListTOAnswerList(subjectJudgeList);
        SubjectOptionBO subjectOptionBO=new SubjectOptionBO();
        subjectOptionBO.setOptionList(subjectAnswerBOS);
        return subjectOptionBO;

    }
}
